layui.use(['layer', 'form', 'jquery', 'upload'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var $ = layui.jquery;
    var upload = layui.upload;
    //获取用户id
    var userId = localStorage.getItem('createId');
    //查询用户信息
    $.ajax({
        url: '/login/getUserById',
        type: 'post',
        dataType: 'json',
        data: {'userId': userId},
        success: function (data) {
            var acatarUrl = "http://" + data.data.acatarUrl;
            $('#acatarUrl').attr('src',acatarUrl);
            $('input[name=acatarUrl]').val(data.data.acatarUrl);
            $('input[name=account]').val(data.data.account);
            $('input[name=email]').val(data.data.email);
        }
    });
    //提交修改密码
    form.on('submit(sub)', function (data) {
        if (data.field.newPassword == data.field.password) {
            $.ajax({
                url: '/login/updateUser',
                type: 'post',
                dataType: 'json',
                data: {'userId': userId, 'password': data.field.password},
                success: function (data) {
                    if (data.data) {
                        layer.msg('修改成功');
                        $('input[name=newPassword]').val('');
                        $('input[name=password]').val('');
                    }
                }
            });
        } else {
            layer.msg('两次的密码不一样');
        }
        return false;
    });
    //修改头像
    upload.render({
        elem: '#acatarUrl',
        url: '/upload/uploadFile',
        field: 'files',
        accept: 'images',
        auto: true,
        acceptMime: 'images/*',
        before: function (obj) {
            //预读本地文件
            obj.preview(function (index, file, result) {
                $('#acatarUrl').attr('src', result);
                $('#acatarUrl').attr('alt', file.name);
                parent.$('#acatarUrl').attr('src', result);
            });
        },
        done: function (res) {
            if (res.code == 0) {
                $('input[name=acatarUrl]').val(res.data.path);
                $.ajax({
                    url: '/login/updateUser',
                    type: 'post',
                    dataType: 'json',
                    data: {'userId': userId, 'acatarUrl': res.data.path},
                    success: function (data) {
                        if (data.data) {
                            layer.msg('修改头像成功');
                        }
                    }
                });
            }
        }
    });
    //校验密码
    form.verify({
        pass: [
            /^[\S]{6,12}$/,
            '密码必须6到12位且不能出现空格'
        ]
    });
    //刷新
    $('#refresh').on('click', function () {
        location.reload();
    })
    //返回
    $('#goBack').on('click', function () {
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    })
});